package com.smartHome.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    //public static final String ALI_DOMAIN = "https://zph001-test.oss-cn-guangzhou.aliyuncs.com/";
   // public static final String ALI_DOMAIN = "https://big-event-program.oss-cn-beijing.aliyuncs.com/";
    public static final String ALI_DOMAIN = "https://aq-guangzhou.oss-cn-guangzhou.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + ext;
        String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
        //String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        //String accessKeyId = "LTAI5tHmyLrQBmVCT8EVn3Ey";
        //String accessKeyId = "LTAI5tEiQdunonFRqdeyotjc";
        String accessKeyId = "LTAI5t5gPUc9obcHdAs1L8Kq";
        //String accessKeySecret = "ZL5WhAxvBH4wRjcyqXvO0Rd9YxNXNM";
        //String accessKeySecret = "VLJkJF1GBihgNbU5ElCjon71wIdVV5";
        String accessKeySecret = "uZT0O4QFqDK9k3S4A99lmTRDPkQUmx";
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

//        ossClient.putObject(
//                "zph001-test",
//                fileName,
//                file.getInputStream()
//        );
       /* ossClient.putObject(
                "big-event-program",
                fileName,
                file.getInputStream()
        );*/
        ossClient.putObject(
                "aq-guangzhou",
                fileName,
                file.getInputStream()
        );

        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }

}
